package com.word;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
	public static void main(String[] args) {
		String t1 = "2020-7-29 00:00:00";
		String t2 = "2020-7-28 5:2:1";
//		if(compareDate(t1,t2)>0){
//			System.out.println("终保日期不能早于起保日期，请重新输入终保日期。");
//		}
//		compareDatenew(t1,t2);
//		compareDatenew1(t1,t2);
		
		if(isDate("4674")){
			System.out.println("正确的日期格式");
		}
	}
	
	public static boolean isDate(String strDate) {
        Pattern pattern = Pattern
                .compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
        Matcher m = pattern.matcher(strDate);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }
	
	public static boolean checkStrFormat(String str) {
		Pattern pattern = Pattern.compile("^([0-9]{4})((0([1-9]{1}))|(1[0-2]))(([0-2]([0-9]{1}))|(3[0|1]))(([0-1]([0-9]{1}))|(2[0-4]))([0-5]([0-9]{1}))([0-5]([0-9]{1}))");
		Matcher matcher = pattern.matcher(str);
		boolean bool = matcher.matches();
		return bool;
		}
  
	public static boolean isDateString(String datevalue) {
		try {
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			Date dd = fmt.parse(datevalue);
			System.out.println("正确的日期格式1");
			System.out.println(fmt.format(dd));
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
  public static int compareDate(String startdate, String anddate) {
        
        
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            Date dt1 = df.parse(startdate);
            Date dt2 = df.parse(anddate);
            System.out.println("dt1="+dt1+" ,dt2="+dt2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt2在dt1前");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
  
  
  public static int compareDatenew(String startdate, String anddate) {
      
      
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      try {
          Date dt1 = df.parse(startdate);
          Date dt2 = df.parse(anddate);
         if(dt2.before(dt1)){
        	 System.out.println("compareDatenew终保日期不能早于起保日期，请重新输入终保日期。");
         }
      } catch (Exception exception) {
          exception.printStackTrace();
      }
      return 0;
  }

  public static void compareDatenew1(String startdate, String anddate) {
      
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      try {
          Date dt1 = df.parse(startdate);
          Date dt2 = df.parse(anddate);
         if(dt2.before(dt1)){
        	 System.out.println("终保日期不能早于起保日期，请重新输入终保日期。");
         }
      } catch (Exception exception) {
          exception.printStackTrace();
      }
 }
}
